package com.lemon.smartsafetynew.ui.adapter.record;


import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.EmerPlanVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

import static com.thefinestartist.utils.content.ContextUtil.startActivity;

/**
 * Created by JiangAn on 2018/6/6.
 */

public class EmergencyPlanAdapter extends LemonRcvAdapter<EmerPlanVo> {
    private RecycleChildItemClickListener<EmerPlanVo> mListener;

    public EmergencyPlanAdapter(@Nullable List<EmerPlanVo> data, RecycleChildItemClickListener<EmerPlanVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new EmergencyPlanAdapter.Item();
    }

    public class Item implements AdapterItem<EmerPlanVo> {

        @Bind(R.id.tv_record_emergency_charge)
        TextView tvRecordEmergencyCharge;
        @Bind(R.id.tv_record_emergency_charge_phone)
        TextView tvRecordEmergencyChargePhone;
        @Bind(R.id.tv_emergency_plan_file)
        TextView tvEmergencyPlanFile;
        @Bind(R.id.tv_start_time)
        TextView tvStartTime;
        @Bind(R.id.tv_download_file)
        TextView tvDownloadFile;
        @Bind(R.id.tv_contact_people)
        TextView tvContactPeople;
        @Bind(R.id.item_layout)
        LinearLayout itemLayout;
        private EmerPlanVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_emergency_plan_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            tvContactPeople.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data,pos);
                }
            });

            tvDownloadFile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onChildItemClick(data,pos);
                }
            });
        }

        @Override
        public void handleData(EmerPlanVo emerPlanVo, int position) {
            data = emerPlanVo;
            pos = position;
            tvRecordEmergencyCharge.setText(CommonUtil.avoidNullString(emerPlanVo.getChargeMan()));
            tvRecordEmergencyChargePhone.setText(CommonUtil.avoidNullString(emerPlanVo.getPhone()));
            tvEmergencyPlanFile.setText(CommonUtil.avoidNullString(emerPlanVo.getFileName()));
            if(!TextUtils.isEmpty(emerPlanVo.getStatus())){
                String startTime = CommonUtil.secondToTimeStr(emerPlanVo.getUpDate(), DateTypeEnum.DAY);
                String banTime = CommonUtil.secondToTimeStr(emerPlanVo.getBanDate(), DateTypeEnum.DAY);
                if(emerPlanVo.getStatus().equals("1")){
                    itemLayout.setBackgroundResource(R.color.white);
                    tvStartTime.setText(startTime+" ~ 至今");
                    tvContactPeople.setTextColor(Color.parseColor("#488cd1"));
                    tvContactPeople.setEnabled(true);
                }else if(emerPlanVo.getStatus().equals("0")){
                    itemLayout.setBackgroundResource(R.color.color_grey_bg);
                    tvStartTime.setText(startTime+" ~ "+banTime);
                    tvContactPeople.setTextColor(Color.parseColor("#999999"));
                    tvContactPeople.setEnabled(false);
                }
            }
        }
    }
}

